package com.xiaotu.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class GetUserUtil {
    public  static String getUser2(){
        //获取应用上下文
        SecurityContext context = SecurityContextHolder.getContext();
        //System.out.println(context);
        //获取用户信息
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        String username = principal.getUsername();
        return username;
    }
}
